package cn.wolfcode.shop.service.impl;

import cn.wolfcode.shop.domain.Good;
import cn.wolfcode.shop.domain.SeckillGood;
import cn.wolfcode.shop.exception.GlobalException;
import cn.wolfcode.shop.mapper.SeckillGoodMapper;
import cn.wolfcode.shop.mq.MQSender;
import cn.wolfcode.shop.redis.RedisServer;
import cn.wolfcode.shop.redis.SeckillGoodKeyPrefix;
import cn.wolfcode.shop.result.CodeMsg;
import cn.wolfcode.shop.service.IGoodService;
import cn.wolfcode.shop.service.ISeckillGoodService;
import cn.wolfcode.shop.vo.GoodVo;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.jms.JMSException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SeckillGoodServiceImpl implements ISeckillGoodService{
    @Autowired
    private SeckillGoodMapper seckillGoodMapper;
    @Reference
    private IGoodService goodService;
    @Autowired
    private RedisServer redisServer;
    @Autowired
    private MQSender mqSender;
    @Override
    public List<GoodVo> selectAll() {
        //查询出所有的秒杀商品
        List<SeckillGood> seckillGoods = seckillGoodMapper.query();
        if(seckillGoods == null){
            return null;
        }
        return createGoodVos(seckillGoods);
    }
    @Transactional
    public List<GoodVo> createGoodVos(List<SeckillGood> seckillGoods) {
        Map<Long,SeckillGood> map = new HashMap<>();
        //将秒杀商品的goodId 作为map的key,seckillGood作为value存进map
        for (SeckillGood seckillGood : seckillGoods) {
            map.put(seckillGood.getGoodId(),seckillGood);
        }
        //获取map的key的集合
        List ids = new ArrayList(map.keySet());
        List<Good> goods = goodService.queryByIds(ids);
        List<GoodVo> goodVoList = new ArrayList<>();
        SeckillGood sg;
        for (Good good : goods) {
            //商品的id,和秒杀商品中的goodId相同 则是同一商品 关联关系成立 实行聚合操作
            sg = map.get(good.getId());
            GoodVo vo = new GoodVo();
            vo.setId(sg.getId());
            vo.setGoodId(good.getId());
            vo.setSeckillPrice(sg.getSeckillPrice());
            vo.setStockCount(sg.getStockCount());
            vo.setStartDate(sg.getStartDate());
            vo.setEndDate(sg.getEndDate());
            vo.setGoodName(good.getGoodName());
            vo.setGoodTitle(good.getGoodTitle());
            vo.setGoodImg(good.getGoodImg());
            vo.setGoodDetail(good.getGoodDetail());
            vo.setGoodPrice(good.getGoodPrice());
            vo.setGoodStock(good.getGoodStock());
            goodVoList.add(vo);
        }
        return goodVoList;
    }

    /**
     * 单个的 聚合对象
     * @param goodId
     * @return
     */
    @Override
    public GoodVo get(Long goodId) {
        List<SeckillGood> seckillGoods = seckillGoodMapper.selectById(goodId);
        if(seckillGoods == null){
            return null;
        }
        return createGoodVos(seckillGoods).get(0);
    }

    @Override
    public GoodVo getCacheByGoodId(Long goodId) {
        return redisServer.hget(SeckillGoodKeyPrefix.SECKILL_GOOD_HASH,"",goodId+"",GoodVo.class);
    }
    @Transactional
    @Override
    public int reduceStockCount(Long goodId) {
        return seckillGoodMapper.reduceStockCountByGoodId(goodId);
    }

    @Override
    public List<GoodVo> getByCache() {
        Map<String, GoodVo> stringGoodVoMap = redisServer.hgetAll(SeckillGoodKeyPrefix.SECKILL_GOOD_HASH, "", GoodVo.class);
        ArrayList<GoodVo> list = new ArrayList<GoodVo>(stringGoodVoMap.values());
        return list;
    }

    @Override
    public void syncStock(Long goodId) throws JMSException {
        int count = seckillGoodMapper.selectStockByGoodId(goodId);
        if(count > 0){
            redisServer.set(SeckillGoodKeyPrefix.SECKILL_GOOD_STOCK,goodId+"",count);
            //发布消息,让订阅者更新本地标记
            mqSender.publishCancelLocalSign(goodId);
        }
    }

}
